#include "Shop.h"

Shop::Shop(Player &p):p(p)
{
    this->SetSymbol("🛒"); //不能怪兽重
    PropVec.push_back(std::make_shared<Weapon>("剑", 10, 10, 0, 0, "武器"));
    PropVec.push_back(std::make_shared<Weapon>("刀", 20, 20,0.2,0,"武器"));
    PropVec.push_back(std::make_shared<Weapon>("棍", 30, 30,0.1,0.1,"武器"));
    PropVec.push_back(std::make_shared<Armor>("布甲",10,2,0.2,"防具"));
    PropVec.push_back(std::make_shared<Armor>("皮甲",20,3,0.1,"防具"));
    PropVec.push_back(std::make_shared<Armor>("铠甲", 30, 4,0,"防具"));
    PropVec.push_back(std::make_shared<Potion>("大血瓶",20,20,"药品"));
    PropVec.push_back(std::make_shared<Potion>("中血瓶",10,10,"药品"));
    PropVec.push_back(std::make_shared<Potion>("小血瓶",5,5,"药品"));

}

void Shop::show()
{
    while(1)
    { 
        system("clear");
        std::cout<< "======== ⚔️  欢迎来到商店 🛡 ========\n";
        std::cout<<"1、出售\n";
        std::cout<<"2、购买\n"<<"3、退出"<<std::endl;
        std::cout << "请输入选项: ";
        int choice1 = 0;
        std::cin>> choice1;
        // 检查输入是否成功
        if (std::cin.fail()) {
            // 清除错误标志
            std::cin.clear();
            // 丢弃错误的输入
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "输入非法，请重新输入: \n";
            continue;
        }
        if (choice1 > 3 || choice1 < 0)
        {
            std::cout << "输入非法 请重新输入: \n";
            continue;
        }

        if(choice1==2)
        {
        while(1)
        {
        int i = 0;
        for (; i < PropVec.size(); i++)
        {
            std::cout<< i + 1 << "、";
            PropVec[i]->show(); //多态 调子类
        }
        std::cout << i + 1 << "、退出" <<std::endl; 
        //购买
        std::cout << "请输入想要购买的编号: ";
        int choice2 = 0;
        std::cin>> choice2;
        // 检查输入是否成功
        if (std::cin.fail()) {
            // 清除错误标志
            std::cin.clear();
            // 丢弃错误的输入
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "输入非法，请重新输入: \n";
            continue;
        }
        if (choice2 == i + 1)
        {
            std::cout << "欢迎再次光临\n";
            sleep(1);
            return;
        }

        if (choice2 > i + 1 || choice2 < 0)
        {
            std::cout << "输入非法 请重新输入: \n";
            continue;
        }

        //购买道具
        p.SetGloden(p.GetGloden() - PropVec[choice2 - 1]->GetPrice());
        std::cout << "恭喜购买了: " << PropVec[choice2 - 1]->GetName() << " " <<std::endl;
        p.InsertBag(PropVec[choice2 - 1]);
        sleep(1);
        }
        }
        else if(choice1==1)
        {
        while(1)
        {
        std::cout << "========背包========\n" << std::endl;
        auto bag=p.Getbag();
        int i = 0;
        for (; i <bag.size() ; i++)
        {
            std::cout<< i + 1 << "、";
            bag[i]->show(); //多态 调子类
        }
        std::cout << i + 1 << "、" << "退出" << std::endl;
        std::cout << "请选择你要操作的物品" <<std::endl;
        int choice3 = 0;
        std::cin >> choice3;
        // 检查输入是否成功
        if (std::cin.fail()) {
            // 清除错误标志
            std::cin.clear();
            // 丢弃错误的输入
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "输入非法，请重新输入: \n";
            continue;
        }
        if (choice3 == i + 1)
        {
            std::cout << "欢迎再次光临\n";
            sleep(1);
            return;
        }

        if (choice3 > i + 1 || choice3 < 0)
        {
            std::cout << "输入非法 请重新输入: \n";
            continue;
        }

        //购买道具
        p.SetGloden(p.GetGloden() + bag[choice3 - 1]->GetPrice()*0.8);
        std::cout << "出售了: " << bag[choice3 - 1]->GetName() << " " <<std::endl;
        p.PopBag(choice3);
        sleep(1);
        }
        }
        else 
        {
            return;
        }



    }

}